<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>serialize方式说明</title>
</head>

<body>
    <form id="form">
        <input type="text" name="username">
        <input type="password" name="password">
        <input type="submit" value="提交">
    </form>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        $('#form').on('submit', function () {
            //将表单内容拼接成字符串类型的参数
            var params = $("#form").serialize();
            // console.log(params);
            let data = serializeobject($(this))
            console.log(data)
            return false;
        })
        //将表单中用户输入的内容转换为对象类型
        function serializeobject(obj) {
            //处理结果对象
            var result = {};
            //[{name: 'username ' , value: '用户输入的内容}，{name: 'passwordvalue: '123456'}]
            var params = obj.serializeArray();
            // 循环数组将数组转换为对象
            $.each(params, function (index, value) {
                result[value.name] = value.value;
            })
            // console.log(result)
            return result;
        }
    </script>
</body>

</html>